package com.lg.atp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.CaseRunDetail;
import com.lg.atp.entity.CaseRunResult;
import com.lg.atp.service.IRunCaseService;

@RestController
@RequestMapping("caseRun")
public class CaseRunController {

	private static final Log log = LogFactory.getLog(CaseRunController.class);
	
	@Autowired
	private IRunCaseService iRunCaseService;
	
	@GetMapping("tolist")
	public ModelAndView toList(Long taskId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("caseRunList");
        view.addObject("taskId", taskId);
		return view;
	}
	
	@GetMapping("toresult")
	public ModelAndView toResult(String json) {
		ModelAndView view = new ModelAndView();
		view.setViewName("caseRunResult");
        CaseRunDetail cd = JSONObject.parseObject(json, CaseRunDetail.class);
        view.addObject("cd", cd);
		return view;
	}
	
	@PostMapping("list")
	public Map<String,Object> getCaseList(Long taskId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<CaseRunDetail> list = iRunCaseService.getRunDetail(taskId);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取执行进度失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取执行进度失败");
		}
		return result;
	}
	
	
	@PostMapping("result")
	public Map<String,Object> getRunResult(Long runId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<CaseRunResult> list = iRunCaseService.getRunResult(runId);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取执行结果失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取执行结果失败");
		}
		return result;
	}
}
